//
//  NRWWebViewController.m
//  NRWMall
//
//  Created by 邢毅 on 2018/6/21.
//  Copyright © 2018年 com.anpxd. All rights reserved.
//

#import "NRWWebViewController.h"
#import <WebKit/WebKit.h>

@interface NRWWebViewController ()<WKNavigationDelegate>

@property (strong, nonatomic) WKWebView *webView;

@property (assign, nonatomic) BOOL isRemoteSource;

@end

@implementation NRWWebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, getNavHeight(), SCREEN_WIDTH(), SCREEN_HEIGHT() - getNavHeight())];
    self.webView.backgroundColor = [UIColor whiteColor];
	self.webView.navigationDelegate = self;
    [self.view addSubview:self.webView];
	
	self.navigationView.title = @"加载中";

    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.url]]];
}

- (void)setUrl:(NSString *)url {
    
    _url = url;
    
    self.isRemoteSource = [url hasPrefix:@"http"];
}

- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation {
	
	[webView evaluateJavaScript:@"document.querySelector('title').innerHTML" completionHandler:^(id _Nullable obj, NSError * _Nullable error) {
		
		if (isEmptyString(obj)) {
			self.navigationView.title = @"网页";
			return ;
		}
		self.navigationView.title = obj;
	}];
}

@end
